# --*-- coding:utf-8 --*--

import warnings
warnings.filterwarnings('ignore')
import requests
import datetime
import time
import json
from multiprocessing import Pool
from multiprocessing.dummy import Pool
import random
import math



class Server_indicator_data():

	cookies = None

	def dateRanges(self,beginDate, endDate):
		dates = []
		dt = datetime.datetime.strptime(beginDate, '%Y-%m-%d')
		date = beginDate[:]
		while date <= endDate:
			dates.append(date)
			dt = dt + datetime.timedelta(1)
			date = dt.strftime('%Y-%m-%d')
		return dates


	def server_data(self,dateRange):	

		# 时间处理
		dateRange = str(dateRange) + '|' + str(dateRange)

		url = 'https://sycm.taobao.com/qos/claim/general/guide.json'
		head = {
			'Host': 'sycm.taobao.com',
			'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
		
		data = {
				'dateType':'day',
				'dateRange':dateRange,
				'_':'1536830092016',
				'token':'d0a221f59'
		}
		time.sleep(random.uniform(1,5))
		resp = requests.get(url,headers = head,verify=False,params = data,cookies = self.cookies)
		return resp.json()
		# try:
		# 	code = resp.json()['code']
		# 	if code == 0:
		# 		return resp.json()
		# 	elif code == 1003:
		# 		return resp.json()
		# 	elif code == 5924: #没有权限
		# 		return return resp.json()
		# except:
		# 	return {'status':False,'msg':'','result':'未知错误'}

	def get_server_data(self):

		# 默认30天

		start_time = (str(datetime.datetime.now() - datetime.timedelta(days = 1)).split(' ')[0])
		end_time = (str(datetime.datetime.now() - datetime.timedelta(days = 30)).split(' ')[0])
		time_list = self.dateRanges(end_time,start_time)
		pool = Pool(5)
		data = pool.map(self.server_data,time_list)
		pool.close()
		pool.join()
		try:
			code = data[0]['code']
			if code == 0:
				return data
			elif code == 1003: # 时间参数错误
				return data[0]
			elif code == 5924: #没有权限
				return data[0]
		except:
			return {'status':False,'msg':'','result':'未知错误'}

	def Any_day_data(self,dates):

		# 任意N天数据

		time_list = self.dateRanges(dates['start_time'],dates['end_time'])
		pool = Pool(math.ceil(len(time_list)/5))
		data = pool.map(self.server_data,time_list)   # 直接查看返回数据判断cookies是否可用
		pool.close()
		pool.join()
		try:
			code = data[0]['code']
			return data
		except:
			return {'status':False,'msg':'','result':'未知错误'}

if __name__ == '__main__':


	Server_indicator_data = Server_indicator_data()

	# 过期cookie
	# Server_indicator_data.cookies =  {'cookie2': '1cbc27af4ccf5ade0fb542b96cd5cc6c', 'v': '0', '_tb_token_': '585b3a55863bf', 'tracknick': '%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97', 'skt': 'a3784400dc6dd807', 'uc3': 'vt3=F8dByRuTL7XRi4qKdBo%3D&id2=UNN0nnQ5PzBj6g%3D%3D&nk2=sHmGuBnK6YrEuQ%3D%3D&lg2=URm48syIIVrSKA%3D%3D', 'dnk': '%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97', 'sg': '%E5%BA%9702', '_nk_': '%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97', 'cna': '62QpFLbO/WECAW/myrjVkBiA', 'uc1': 'cookie16=UtASsssmPlP%2Ff1IHDsDaPRu%2BPw%3D%3D&tmb=1&cookie21=U%2BGCWk%2F7oPGl&cookie15=VT5L2FSpMGV7TQ%3D%3D&existShop=true&pas=0&cookie14=UoTfLJy2VQ5MAA%3D%3D&tag=8&lng=zh_CN', 'tg': '0', '_l_g_': 'Ug%3D%3D', '_cc_': 'UtASsssmfA%3D%3D', 'lgc': '%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97', 'apushd2dce1c5dd89f161d480bff397a6184e': '%7B%22ts%22%3A1537942583184%2C%22parentId%22%3A1537942582180%7D', 'cookie1': 'UNaOmjEdkzPF0d%2BKqRdPBjspDZcJ%2BrbnVjjQlzW63YA%3D', 'unb': '3397628220', 'existShop': 'MTUzNzk0MjU3OQ%3D%3D', 'cookie17': 'UNN0nnQ5PzBj6g%3D%3D', 'JSESSIONID': '575585D81E3FF964C36B3E45411CCA0D', 'isg': 'BJiYCgn5d9Aq41tUoY1-7_q3Zc8qgfwLa-omuNKJ5FOGbThXepHMm67PoSvd5rTj', 't': '54c9e5b360c7a3789263e64f50dfbe45', 'csg': '4c4b18a3', 'mt': 'np='}
	
	# 没权限cookie
	# Server_indicator_data.cookies = {
 #        "unb": "3966986640",
 #        "cna": "yT1EFFgLEWoCAcFwhT6Vrka3",
 #        "v": "0",
 #        "x": "2456118178",
 #        "sn": "%E6%AC%A3%E7%BE%8E%E9%9B%85%E6%97%97%E8%88%B0%E5%BA%97%3A%E8%BE%B0%E6%88%98",
 #        "csg": "246e73d8",
 #        "t": "7b65da1f7e79c481a4392b0905397ccf",
 #        "_tb_token_": "f8ea33efeeb07",
 #        "uc1": "cookie14=UoTfItcIkHFDaA%3D%3D&lng=zh_CN",
 #        "apusha5e68abda32918cbf95a295c9663849c": "%7B%22ts%22%3A1539744548564%2C%22parentId%22%3A1539744443478%7D",
 #        "uc3": "id2=&nk2=&lg2=",
 #        "isg": "BEFBvOY2_lbtKRJiseKjwMWgXIRbbrVg6u2vQ6OWPcinimFc677FMG8IaHDMmU2Y",
 #        "tracknick": "",
 #        "skt": "cbf5dbbfb8108bfb",
 #        "cookie2": "1bc556edb9c0583f0adebb8d568d7922"
 #    }


	# 保存失败cookies  
	Server_indicator_data.cookies = {
        "x": "3302273460",
        "uc1": "cookie14=UoTYNkL%2BQLpNDA%3D%3D&lng=zh_CN",
        "csg": "989c2201",
        "t": "9f74b25416f8f0df5749716a073cfd32",
        "sn": "%E4%B8%87%E9%80%9A%E5%8C%BB%E7%96%97%E7%94%A8%E5%93%81%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8%3A%E7%82%B9%E5%B0%86",
        "_m_h5_tk_enc": "a3ff95f457d047c8ce88d547772b509e",
        "tracknick": "",
        "thw": "gb",
        "_mw_us_time_": "1540521101450",
        "_tb_token_": "70b8338186b53",
        "isg": "BAUFcJVs8lYPttYKC2A9SVToGEg_wrlUNnlrnwdqwTxLniUQzxLJJJPsrkxoxdEM",
        "uc3": "id2=&nk2=&lg2=",
        "v": "0",
        "apushed003dd962a5d68fbb44d1296431eff0": "%7B%22ts%22%3A1540521114333%2C%22parentId%22%3A1540521103318%7D",
        "unb": "4203319857",
        "cna": "cJFTFKDA9n4CAcFwhT5/XTa9",
        "cookie2": "191cff177077e271dcaa2275056e5458",
        "_m_h5_tk": "cd6375ca468a93fde7518736905758d5_1540529383623",
        "skt": "273623d2c3d2bb31"
    }

    # 御果
    # Server_indicator_data.cookies = {
    #     "_l_g_": "Ug%3D%3D",
    #     "existShop": "MTU0MDUxNzgwMA%3D%3D",
    #     "v": "0",
    #     "tracknick": "%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97",
    #     "t": "fff1788327253796e6040cd62742c8d7",
    #     "csg": "c7171aac",
    #     "uc1": "cookie16=UIHiLt3xCS3yM2h4eKHS9lpEOw%3D%3D&tmb=1&cookie21=WqG3DMC9EmWL&cookie15=VT5L2FSpMGV7TQ%3D%3D&existShop=true&pas=0&cookie14=UoTYNkL9Hdrw2Q%3D%3D&tag=8&lng=zh_CN",
    #     "lgc": "%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97",
    #     "dnk": "%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97",
    #     "cookie2": "1099f2f61065b226c831a70524036cc5",
    #     "tg": "0",
    #     "skt": "4fe21cba3145a5f5",
    #     "_cc_": "Vq8l%2BKCLiw%3D%3D",
    #     "cna": "cJFTFKDA9n4CAcFwhT5/XTa9",
    #     "_tb_token_": "e0e7e3767b605",
    #     "cookie17": "UNN0nnQ5PzBj6g%3D%3D",
    #     "unb": "3397628220",
    #     "JSESSIONID": "9189853544208D4AE5D2B65E74A0479E",
    #     "uc3": "vt3=F8dByRjKAd1EZPP7buw%3D&id2=UNN0nnQ5PzBj6g%3D%3D&nk2=sHmGuBnK6YrEuQ%3D%3D&lg2=U%2BGCWk%2F75gdr5Q%3D%3D",
    #     "isg": "BL6-wXGzabsrmb2f92kKouheA90A_4J5IczAamjHKoH8C17l0I_SieRph1nHKHqR",
    #     "sg": "%E5%BA%9702",
    #     "_nk_": "%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97",
    #     "cookie1": "UNaOmjEdkzPF0d%2BKqRdPBjspDZcJ%2BrbnVjjQlzW63YA%3D",
    #     "mt": "np="
    # }


	dates = {"start_time":'2018-10-01',"end_time":'2018-10-01'}

	print (Server_indicator_data.Any_day_data(dates))

	

# {'apush698b6ba7330b40cbf3b99f896fcd909d': '%7B%22ts%22%3A1536887245536%2C%22parentId%22%3A1536887244532%7D', 'uc1': 'cookie14=UoTfLQuBDN0G1w%3D%3D&lng=zh_CN', 'cookie2': '1151a4542f06daaf3d7f241f8a663f7d', 'unb': '3199442953', 'skt': 'e5b5ed54e618476d', 'tracknick': '', 'isg': 'BC4udbFG-YI9rw1mgaMIvCBNc43wL_IpMfxQOlj3mjHsO86VwL9COdS59ynX-OpB', 'x': '2235708395', 'v': '0', '_tb_token_': '35037e8e5b988', 'sn': '%E5%B0%8F%E7%8B%AE%E5%AD%90%E7%8E%A9%E5%85%B7%E4%B8%93%E8%90%A5%E5%BA%97%3A%E6%8E%A8%E5%B9%BF04', 'csg': 'f6d42421', 'cna': '5dbzE7zfsnwCAW/myrh+5F+Z', 't': 'c4aa9eb41f2e4538abceecb59c25311c', 'uc3': 'id2=&nk2=&lg2='}

# {'uc1': 'cookie14=UoTfLQuBDNvjJw%3D%3D&lng=zh_CN', 'cookie2': '1af98d91903e5877e3237fdf3175a01e', 'unb': '3416683071', 'skt': '885fbbadcb7d1276', 'tracknick': '', 'v': '0', 'x': '2878767477', 'isg': 'BFRUA5_p85xgS2fQlFgDghkNKZvGrXiXj9767O414F9i2fQjFr1IJwrb3V_BOrDv', '_tb_token_': '5553b670e133b', 'sn': 'chailtee%E9%A6%A8%E7%BC%87%E5%90%89%E5%AE%B6%E5%B1%85%E6%97%97%E8%88%B0%E5%BA%97%3A%E8%89%BA%E9%A3%8E', 'csg': '7ff02bed', 'cna': '5dbzE7zfsnwCAW/myrh+5F+Z', 't': '2a4c681c4055fba7ec167289ccf49dc5', 'uc3': 'id2=&nk2=&lg2=', 'apush481ea92b828a3c9ce9ed7c72f58bb0ea': '%7B%22ts%22%3A1536887439649%2C%22parentId%22%3A1536887434424%7D'}


